import {AxdrSequenceOf} from "../asn1.axdr/AxdrSequenceOf";
import {OAD} from "./OAD";
import {Buffer} from "buffer";
import {TSA} from "./TSA";
import {Unsigned16} from "./Unsigned16";
import {AxdrType} from "../asn1.axdr/AxdrType";
import {ReverseByteArrayInputStream} from "../ReverseByteArrayInputStream";
import {ReverseByteArrayOutputStream} from "../ReverseByteArrayOutputStream";


export class SubSeqOfOads extends AxdrSequenceOf<OAD>{

    createListElement(): OAD {
        return new OAD();
    }

    constructor() {
        super();
    }

    set_length(length: number) {
        super.set_length(length);
    }

}


export class ProxyReadOads implements AxdrType{

    dataCode : Buffer | null = null;
    tsa : TSA | null = null;
    waitTime : Unsigned16 | null = null;
    oads : SubSeqOfOads | null = null;

    constructor() {
    }

    set_dataCode(dataCode : Buffer){
        this.dataCode = dataCode;
    }

    set_all(tsa : TSA, waitTime : Unsigned16, oads : SubSeqOfOads){
        this.tsa = tsa;
        this.waitTime = waitTime;
        this.oads = oads;
    }

    setTsa(tsa : TSA){
        this.tsa = tsa;
    }

    setWaitTime(waitTime : Unsigned16){
        this.waitTime = waitTime;
    }

    setOads(oads : SubSeqOfOads){
        this.oads = oads;
    }

    decode(input: ReverseByteArrayInputStream): number {
        let codeLength = 0;

        this.tsa = new TSA();
        codeLength += this.tsa.decode(input);

        this.waitTime = new Unsigned16();
        this.waitTime.set_const();
        codeLength += this.waitTime.decode(input);

        this.oads = new SubSeqOfOads();
        codeLength += this.oads.decode(input);

        return codeLength;

    }

    encode(output: ReverseByteArrayOutputStream): number {
        let codeLength;

        if (this.dataCode != null) {
            codeLength = this.dataCode.length;
            for (let i = this.dataCode.length - 1; i >= 0; i--) {
                output.write(this.dataCode[i]);
            }
        } else {
            if(this.tsa != null && this.waitTime != null && this.oads != null){
                codeLength = 0;
                codeLength += this.oads.encode(output);
                codeLength += this.waitTime.encode(output);
                codeLength += this.tsa.encode(output);
            }else{
                throw new Error("tsa || waitTime || oads is null");
            }
        }
        return codeLength;
    }

    encodeAndSave(encodingSizeGuess: number) {
        let revOStream = new ReverseByteArrayOutputStream();
        revOStream.setBufSize(encodingSizeGuess);
        this.encode(revOStream);
        this.dataCode = revOStream.getArray();
    }

    toString(): string {
        if (this.tsa != null && this.waitTime != null && this.oads != null) {
            return "sequence: {"+ "tsa: " + this.tsa + ", waittime: " + this.waitTime + ", oads: " + this.oads  + "}";
        } else {
            return "tsa || waitTime || oads is null";
        }
    }


}
